package com.icss.ah.login.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.login.bean.UserInfoBean;
/**
 * 公告提醒
 * @author csadchen
 * @date 2017-07-21
 */
@Controller
public class TipsAction extends BaseAction{

	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	private UserInfoBean userInfo;		//用户信息
	/**
	 * 进入通知中心列表页面
	 * @return
	 */
	public String tipListPage(){
		return "tipListPage";
	}
	/**
	 * 获取通知中心列表
	 * @return
	 */
	public void queryTipsListPage(){
		request = getRequest();
		response = getResponse();
		dataGridJson = new EasyuiDataGridJson();
		try {
			//获取登录账号信息
			userInfo = getUserInfo("");
			//查询参数
			HashMap map = new HashMap();
			map.put("shopId", userInfo.getShopId());
			map.put("readPerson", userInfo.getUserId());
			map.put("startIndex",getStartIndexMysql());
			map.put("endIndex",getEndIndexMysql());
			//记录日志
			infolog.info("TipsAction|tipListPage|map:"+map);
			//公告提醒信息列表
			dataGridJson = tipsService.queryTipsListPage(map);
		} catch (Exception e) {
			errlog.error("TipsAction|tipListPage：",e);
		} finally{
			str2Resp(JSONObject.fromObject(dataGridJson).toString());
		}
	}
	
	
	/**
	 * 获取公告提醒信息
	 */
	public void queryTipsList(){
		request = getRequest();
		response = getResponse();
		jsonText = new Json();
		try {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=UTF-8");
			//获取登录账号信息
			userInfo = getUserInfo("");
			//查询参数
			HashMap map = new HashMap();
			map.put("shopId", userInfo.getShopId());
			map.put("readPerson", userInfo.getUserId());
			//记录日志
			infolog.info("TipsAction|queryTipsList|map:"+map);
			//公告提醒信息列表
			List list = tipsService.queryTipsList(map);
			//记录日志
			infolog.info("TipsAction|queryTipsList|list:"+list.size());
			//返回公告提醒信息
			jsonText.setSuccess(true);
			jsonText.setObj(list);
		} catch (Exception e) {
			errlog.error("TipsAction|queryTipsList异常：",e);
			jsonText.setSuccess(false);
			jsonText.setMsg(e.getMessage());
		} finally{
			JSONObject json = JSONObject.fromObject(jsonText);
			try {
				//公告提醒信息
				response.getWriter().print(json.toString());
			} catch (IOException e) {
				errlog.error("TipsAction|queryTipsList|print异常：",e);
			}
			
		}
	}
	/**
	 * 保存公告提醒阅读信息
	 */
	public void readTips(){
		request = getRequest();
		response = getResponse();
		jsonText = new Json();
		try {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=UTF-8");
			//获取登录账号信息
			userInfo = getUserInfo("");
			//获取需要更改已读状态的公告提醒信息ID
			String tipIds = request.getParameter("tipIds");
			tipIds = tipIds == null ? "" : tipIds;
			//ID处理
			tipIds = tipIds.replaceAll(",", "','");
			//参数
			HashMap map = new HashMap();
			map.put("shopId", userInfo.getShopId());
			map.put("readPerson", userInfo.getUserId());
			map.put("tipIds", tipIds);
			map.put("remark", "关闭弹出层");
			//记录日志
			infolog.info("TipsAction|readTips|map:"+map);
			//更改已读状态
			boolean result = tipsService.readTips(map);
			//记录日志
			infolog.info("TipsAction|readTips|result:"+result);
			//返回结果
			jsonText.setSuccess(result);
		} catch (Exception e) {
			errlog.error("TipsAction|queryTipsList异常：",e);
			jsonText.setSuccess(false);
			jsonText.setMsg(e.getMessage());
		} finally{
			JSONObject json = JSONObject.fromObject(jsonText);
			try {
				//返回结果
				response.getWriter().print(json.toString());
			} catch (IOException e) {
				errlog.error("TipsAction|queryTipsList|print异常：",e);
			}
			
		}
	}
	
	
}
